package com.quanshu.exchange.ui.gui.message.chat.redpackage;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.entity.ReceiveRedEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.DateUtil;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.BaseFragment;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ReceiveRedFragment extends BaseFragment {

    @BindView(R.id.red_package_recored)
    RelativeLayout redPackageRecored;
    @BindView(R.id.red_recy)
    RecyclerView redRecy;

    private List<ReceiveRedEntity> datas = new ArrayList<>();//全部数据
    private SingleAdapter<ReceiveRedEntity> adapter;

    private static BaseActivity mContext;

    public static ReceiveRedFragment newInstance(BaseActivity context) {
        ReceiveRedFragment f = new ReceiveRedFragment();
        Bundle args = new Bundle();
        mContext = context;
        f.setArguments(args);
        return f;
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.fragment_receive_red;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        receiveRed();
    }

    @Override
    protected void initView() {
        redRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SingleAdapter<>(new DataDisplayer<ReceiveRedEntity>(getActivity(),
                R.layout.item_red_package_record) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<ReceiveRedEntity> wrapper,
                                 ReceiveRedEntity item,
                                 int position) {
                holder.setText(R.id.red_package_name, item.getSend_memberid());
                BigDecimal b = new BigDecimal(item.getGet_money());
                double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                holder.setText(R.id.red_package_sum, f1 + "");
                holder.setText(R.id.red_package_time,
                        DateUtil.TimeStamp2Date(item.getGet_time(), "MM-dd HH:mm"));
            }
        });
        redRecy.setAdapter(adapter);
    }

    //领取红包记录Data
    private void receiveRed() {
        NetClient.getInstance().
                getNetService(UserInterface.class)
                .receiveRed(AppUtils.getSignParams(MyApplication.getInstance().getCurrentUser().getMemberid(), "1"),
                        MyApplication.getInstance().getCurrentUser().getMemberid(), "1")
                .compose(new BaseTransformer<>(mContext))
                .subscribe(new BaseObserver<BaseResponse<List<ReceiveRedEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<ReceiveRedEntity>> response) {

                        if (response.getInfo() != null && response.getInfo().size() > 0) {
                            datas.addAll(response.getInfo());
                            if (datas != null && datas.size() > 0) {
                                adapter.setData(datas);
                                redPackageRecored.setVisibility(View.GONE);
                            } else {
                                redPackageRecored.setVisibility(View.VISIBLE);
                            }
                        } else {
                            redPackageRecored.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }


}
